
package StudentSystem;
import java.io.File;
import java.io.*;
import java.util.ArrayList;

public class InformationScanSystem {
    public void Write(String[] gs) {//储存文件数据

        System.out.println("请输入>>");
        try {
            File out=new File("D:\\Desktop\\编程类\\编程\\Java\\experiment-2-master\\StudentSystem\\课程信息.txt");
            FileOutputStream txt=new FileOutputStream(out);
            OutputStreamWriter dos=new OutputStreamWriter(txt);
            for (String g : gs) {
                dos.write(g);
                dos.write('\n');
            }
            dos.close();
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
    public String[] Read(int num,String path) {//外来文件数据
        String[] data=new String[num];
        int num0=0;
        try {

            FileInputStream in=new FileInputStream(path);
            InputStreamReader str= new InputStreamReader(in);
            int len;
            while (str.read()!=-1){
                num0+=1;
            }
            data[0]=(num0+1)/8+"";
            int i=1;
            while ((len=str.read())!=-1){
                if ( (len == 'r')|(len=='\n')) {//换行为分隔
                    i+=1;
                }
                else{
                    data[i]+=len;

                }
            }
            str.close();
        }

        catch(Exception e1){
            e1.printStackTrace();
            System.out.println("文件读取错误");
        }
        return  data;//str放在这种格式的string返还Sdata:{{{学生数}},{学生编号，学生密码，学生姓名，学生性别，{课程编号}，{课程名称}，{课程时间}，{课程地点}}} Tdata:{{{教师数}},{教师编号，教师密码，教师姓名，教师性别，{课程编号}，{课程名称}，{课程时间}，{课程地点}}}



    }
    public void WriteData(int num,String[][][] data,String path,String list_all_list,ArrayList<Data1> ADT) {
        ObjectOutputStream out ;
        ArrayList<Data1> sIn = new ArrayList<>(num);
        try {
            //num写入学生数
            if(list_all_list.equals("list"))
                for(int i=0;i<num;i++) {
                    sIn.add(new Data1(data[i][0][0], data[i][2][0], data[i][3][0], data[i][1][0], data[i][4], data[i][5],data[i][6], data[i][7] ));
                    //Sdata:{{学生编号，学生密码，学生姓名，学生性别，{课程编号}，{课程名称}，{课程时间}，{课程地点}}}
                    //Tdata:{{教师编号，教师密码，教师姓名，教师性别，{课程编号}，{课程名称}，{课程时间}，{课程地点}}}
                    //Data1(String user_id,String name,String sex,String password,String[] course_id,String[] course_name,String[] course_time,String[] course_sit)
                }
            else if(list_all_list.equals("all_list"))
                for(int i=0;i<num;i++) {
                    sIn.addAll(ADT);
                }
            else
                System.out.println("list_class输入错误应为list/class");
            out = new ObjectOutputStream(new FileOutputStream(path));
            out.writeObject( sIn);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }


    public  ArrayList<Data1> ReadData(String path) {
        ObjectInputStream in ;
        ArrayList<Data1> sOut ;
        try {
            in = new ObjectInputStream(new FileInputStream(path));

            sOut = (ArrayList<Data1>)in.readObject() ;

        } catch (ClassNotFoundException | IOException e) {
            throw new RuntimeException(e);
        }
        return sOut;
    }

    static class Data1 implements Serializable {
        int user_id;
        String name,sex,password;

        int[] course_id;
        String[] course_sit,course_time,course_name;

        Data1(String user_id,String name,String sex,String password,String[] course_id,String[] course_name,String[] course_time,String[] course_sit){
            int[] course_id1=new int[course_name.length];

            this.user_id=Integer.parseInt(user_id);
            this.password=password;
            this.name=name;
            this.sex=sex;

            for(int i=0;i<course_id.length;i++)
                course_id1[i]=Integer.parseInt(course_id[i]);
            this.course_id=course_id1;
            this.course_name=course_name;
            this.course_sit=course_sit;
            this.course_time=course_time;
        }

        public String toString() {
            String personal_information;
            StringBuilder course_information= new StringBuilder();
            personal_information=" 编号: "+this.user_id +" 密码: "+this.password +" 姓名: "+this.name +" 性别: "+this.sex+"\n"+"课程编号:               "+"课程名称:               "+"课程时间:               "+"课程地点:              "+"\n";
            for(int i=0;i<course_name.length;i++){
                course_information.append(this.course_id[i]).append("               ").append(this.course_name[i]).append("               ").append(this.course_sit[i]).append("               ").append(this.course_time[i]).append("\n");
            }
            return  personal_information+course_information;
        }


    }}

